package com.cnlbc.Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.cnlbc.Model.BookType;
import com.cnlbc.Util.StringUtil;

public class BookTypeDao {	 
	/**
	 * 添加
	 * @param con
	 * @param bookType
	 * @return
	 * @throws Exception
	 */
	public int add(Connection con, BookType bookType) throws Exception {
		String sql = "insert into bookType values(null,?,?)";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, bookType.getBookTypeName());
		pstmt.setString(2, bookType.getBookTypeDesc());
		return pstmt.executeUpdate();
	}
	/**
	 * 查询图书类别
	 * @param con
	 * @param bookType
	 * @return
	 * @throws Exception 
	 */
	public ResultSet list(Connection con,BookType bookType) throws Exception {
		StringBuffer sb=new StringBuffer("select * from booktype");
		if(StringUtil.isNotEmpty(bookType.getBookTypeName())) {
			 sb.append(" and bookTypeName like '%"+bookType.getBookTypeName()+"%'"); 
		}
		PreparedStatement pstmt=con.prepareStatement(sb.toString().replaceFirst("and","where"));
		return pstmt.executeQuery();
	}
	/**
	 * 删除图书类别
	 * @param con
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public int delete(Connection con,String id) throws Exception {
		String sql="delete from booktype where id=?";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, id);		
		return pstmt.executeUpdate();
	}
	/**
	 * 更新数据类别
	 * @param con
	 * @param bookType
	 * @return
	 * @throws Exception
	 */
	public int updata(Connection con,BookType bookType) throws Exception {
		String sql="update bookType set bookTypeName=?,bookTypeDesc=? where id=?";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1,bookType.getBookTypeName());
		pstmt.setString(2,bookType.getBookTypeDesc());
		pstmt.setInt(3,bookType.getId());
		return pstmt.executeUpdate();
	}
}
